package com.musemun.controller;

import com.musemun.common.CommonResult;
import com.musemun.common.DataVerification;
import com.musemun.common.ResultCode;
import com.musemun.common.ResultUtil;
import com.musemun.exception.ApiException;
import com.musemun.pojo.Crfixifo;
import com.musemun.pojo.Cropifo;
import com.musemun.pojo.Fixcontact;
import com.musemun.pojo.Vo.CrFixLendQueryTimeVo;
import com.musemun.pojo.Vo.CrFixVo;
import com.musemun.service.function.CulturalRelicFix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/crms/fixAdmin")
@Api(tags="文物修复模块")
public class FixModuleController {

    @Resource
    private CulturalRelicFix culturalRelicFix;


    /**
     * //     * @param crfixifo 文物修复详情表
     * //     * @param crfixifoBind
     * //     * @param fixcontact 文物修复联系人表
     * //     * @param fixcontactBind
     * //     * @param cropifo 文物操作记录表
     * //     * @param cropifoBind
     *
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "添加文物修复信息", httpMethod = "POST")
    public CommonResult insert(@Valid @RequestBody CrFixVo crFixVo, BindingResult bindingResult) {
        DataVerification.check(bindingResult);
        boolean flag = culturalRelicFix.insert(crFixVo.getCrfixifo(), crFixVo.getFixcontact(), crFixVo.getCropifo());
        Map<String, Boolean> result = new HashMap<>();
        result.put("flag", flag);
        return ResultUtil.success(result);
    }

    /**
     * @param crNo
     * @return
     */
    @GetMapping("/selectById")
    @ApiOperation(value = "根据文物编号查询", httpMethod = "GET")
    public CommonResult selectById(@RequestParam(value = "crNo", required = false) String crNo) {
        Long crNoL = DataVerification.checkNumLong(crNo);
        return ResultUtil.success(culturalRelicFix.findById(crNoL));
    }

    @GetMapping("/selectAll")
    @ApiOperation(value = "无条件查询(可分页)", httpMethod = "GET")
    public CommonResult selectAll(@RequestParam(value = "pageNum", required = false) String pageNum,
                                  @RequestParam(value = "pageSize", required = false) String pageSize) {
        int pageNumI = DataVerification.checkNumInteger(pageNum);
        int pageSizeI = DataVerification.checkNumInteger(pageSize);
        Map<String, Object> result = new HashMap<>();
        return ResultUtil.success(culturalRelicFix.selectAll(pageNumI, pageSizeI));
    }

    @GetMapping("/selectByName")
    @ApiOperation(value = "根据修复人查询(可分页)", httpMethod = "GET")
    public CommonResult selectByName(@RequestParam(value = "FCName") String FCName,
                                     @RequestParam(value = "pageNum", required = false) String pageNum,
                                     @RequestParam(value = "pageSize", required = false) String pageSize) {
        int pageNumI = DataVerification.checkNumInteger(pageNum);
        int pageSizeI = DataVerification.checkNumInteger(pageSize);
        return ResultUtil.success(culturalRelicFix.findByName(FCName, pageNumI, pageSizeI));
    }

    /**
     * @param crNo
     * @return 返回要修改的信息
     */
    @GetMapping("/update")
    @ApiOperation(value = "返回要修改的信息(GET请求)", httpMethod = "GET")
    public CommonResult update(@RequestParam(value = "crNo", required = false) String crNo) {
//        long crNoL = DataVerification.checkNumLong(crNo);
        return new CommonResult(-1, "禁止访问", "该接口已经停止访问");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改修复信息(POST请求)", httpMethod = "POST")
    public CommonResult update() {

//        boolean flag = culturalRelicFix.update(crFixVo.getCrfixifo(), crFixVo.getFixcontact());
//        Map<String, Object> result = new HashMap<>();
//        result.put("flag", flag);
//        return ResultUtil.success(result);
        return new CommonResult(-1, "禁止访问", "该接口已经停止访问");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除文物信息", httpMethod = "GET")
    public CommonResult delete(@RequestParam(value = "crNO", required = false) String crNo) {
        long crNoL = DataVerification.checkNumLong(crNo);
        boolean flag = culturalRelicFix.delete(crNoL);
        Map<String, Object> result = new HashMap<>();
        result.put("flag", flag);
        return ResultUtil.success(result);
    }

    @PostMapping("/selectByTime")
    @ApiOperation(value = "根据修复起始时间查询", httpMethod = "POST")
    public CommonResult selectByTime(@Validated @RequestBody CrFixLendQueryTimeVo crFixLendQueryTimeVo, BindingResult bindingResult) {
        DataVerification.check(bindingResult);
        List list = culturalRelicFix.findByTime(crFixLendQueryTimeVo.getLeftTime(), crFixLendQueryTimeVo.getRightTime(), crFixLendQueryTimeVo.getOperate());
        Map<String, Object> result = new HashMap<>();
        result.put("flag", true);
        result.put("list", list);
        return ResultUtil.success(result);
    }


}
